import { ElMessage, ElMessageBox, ElLoading } from "element-plus";
import Big from "big.js";
let loadingInstance = null;
const PHONE = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;

let isEmpty = (value) => {
  return value == null || value === undefined || value === "";
};

let prettyPrint = (title, text, color) => {
  console.log(
    `%c ${title} %c ${text} %c`,
    `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`,
    `border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`,
    "background:transparent"
  );
};
const xh = {
  cf: (a, b) => {
    let x = new Big(a);
    let y = x.times(b); // '1.8'
    console.log(Big("7e+500").times(y));
    return Big("7e+500").times(y); // '1.8'
  },

  // 提示删除
  DelMessageBox: (title = "是否删除", type = "warning", fun) => {
    return new Promise((v, j) => {
      ElMessageBox.confirm(`注：${title}`, type, {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          v(true);
        })
        .catch(() => {
          v(false);
        });
    });
  },
  // 提示保存
  SubMessageBox: (title = "是否保存", type = "warning", fun) => {
    return new Promise((v, j) => {
      ElMessageBox.confirm(`注：${title}`, type, {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          v(true);
        })
        .catch(() => {
          v(false);
        });
    });
  },

  FreezMessageBox: (title = "是否解冻/封存", type = "warning", fun) => {
    return new Promise((v, j) => {
      ElMessageBox.confirm(`注：${title}`, type, {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          v(true);
        })
        .catch(() => {
          v(false);
        });
    });
  },
  // 提示审核
  ExamineMessageBox: (title = "是否确定提交审核？", type = "warning", fun) => {
    return new Promise((v, j) => {
      ElMessageBox.confirm(`注：${title}`, type, {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          v(true);
        })
        .catch(() => {
          v(false);
        });
    });
  },
  // 撤销审核提示
  RevokeMessageBox: (title = "是否确定撤销审核？", type = "warning", fun) => {
    return new Promise((v, j) => {
      ElMessageBox.confirm(`注：${title}`, type, {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          v(true);
        })
        .catch(() => {
          v(false);
        });
    });
  },
  //
  MessageBox: (title = "是否", type = "warning", fun) => {
    return new Promise((v, j) => {
      ElMessageBox.confirm(`注：${title}`, type, {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          v(true);
        })
        .catch(() => {
          v(false);
        });
    });
  },

  // 成功提示
  successMsg: (cemment) => {
    ElMessage["success"](cemment);
  },
  // 失败提示
  errorMsg: (cemment) => {
    ElMessage["error"](cemment);
  },
  // 警告提示
  warningMsg: (cemment) => {
    ElMessage["warning"](cemment);
  },
  // 提示 表单提示
  formMsg: () => {
    ElMessage["warning"]("请填写完整");
  },
  // 表单校验 类别编码不为汉字
  noChinese: (rule, value, callback) => {
    if (!/^[^\u4e00-\u9fa5]+$/.test(value))
      return callback(new Error("编码不能包含汉字"));
    callback();
  },

  // 表单验证 ,编码只能由数字、字母、下划线组成
  alphabet: (rule, value, callback) => {
    if (!/^[0-9a-zA-Z_]{1,}$/.test(value))
      return callback(new Error("编码只能由数字、字母、下划线组成"));
    callback();
  },
  // 输入框必须为正整数
  isNumber: (rule, value, callback) => {
    if (!/^[0-9]*[1-9][0-9]*$/.test(value))
      return callback(new Error("请输入正整数"));
    callback();
  },
  // 校验 手机号
  isPhone: (rule, value, callback) => {
    // 没有输入值通过
    if (!value) return callback();
    if (!PHONE.test(value)) return callback(new Error("请正确输入手机号！"));
    callback();
  },
  voicePrompt: (text) => {
    new Audio(
      "http://tts.baidu.com/text2audio?cuid=baiduid&lan=zh&ctp=1&pdt=311&tex=" +
        text
    ).play();
  },
  // 校验只能由数字且有大写字母组成
  bigLetter: (rule, value, callback) => {
    // 没有输入值通过
    if (!value) return callback();
    if (!/^(?=.*\d)(?=.*[A-Z]).+$/.test(value))
      return callback(new Error("只能由数字,大写字母组成"));
    callback();
  },
  // 校验 只能为4位大写字母
  letterFour: (rule, value, callback) => {
    if (!(/[A-Z]/.test(value) && value.length === 4))
      return callback(new Error("只能为4位大写字母"));
    callback();
  },
  // 银行卡号 校验格式
  cardNumber: (rule, value, callback) => {
    let reg = /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/;
    // 没有输入值通过
    if (!value) return callback();
    // 输入值后校验格式
    if (!reg.test(value)) return callback(new Error("请正确输入银行卡号!"));
    callback();
  },

  /**
   * 发票代码 校验数字 12位
   *
   * @function abcRegExp
   * @returns {RegExp} 发票代码
   */
  invoiceCode: (rule, value, callback) => {
    let reg = /^[0-9]*$/;
    // // 没有输入值通过
    // if (!value) return callback()
    // 输入值后校验格式
    if (!reg.test(value) || value.length != 12)
      return callback(new Error("请正确输入发票代码!"));
    callback();
  },
  /**
   * 发票号码 校验数字 8位
   *
   * @function abcRegExp
   * @returns {RegExp} 发票号码
   */
  invoiceNumber: (rule, value, callback) => {
    let reg = /^[0-9]*$/;
    // // 没有输入值通过
    // if (!value) return callback()
    // 输入值后校验格式
    if (!reg.test(value) || value.length != 8)
      return callback(new Error("请正确输入发票号码!"));
    callback();
  },

  /**
   * 纳税人识别号 校验数字 8位
   *
   * @function abcRegExp
   * @returns {RegExp} 纳税人识别号
   */
  identificationNumber: (rule, value, callback) => {
    // 没有输入值通过
    if (!value) return callback();
    if (!/^(?=.*\d)(?=.*[A-Z]).+$/.test(value))
      return callback(new Error("请正确填写纳税人识别号!"));
    callback();
  },

  /**
   * 农业银行卡正则表达式
   *
   * @function abcRegExp
   * @returns {RegExp} 银行卡正则表达式
   */
  abcRegExp: (rule, value, callback) => {
    return /^(622821|622822|622823|622824|622825|95596|95598|95599)\d{10}$/;
  },
  /**
   * 工商银行卡正则表达式
   *
   * @function icbcRegExp
   * @returns {RegExp} 银行卡正则表达式
   */
  icbcRegExp: () => {
    return /^(622202|622230|622231|622232|622233|622234|622235|955888|955880|955881|955882)\d{10}$/;
  },

  // 校验 不能小于1
  check: (value) => {
    if (value < 1) {
      ElMessage["warning"]("此项不能小于1");
      return false;
    } else {
      return true;
    }
  },

  // 打开遮罩层
  loading: (content) => {
    loadingInstance = ElLoading.service({
      lock: true,
      text: content,
      background: "rgba(0, 0, 0, 0.7)",
    });
    console.log(loadingInstance, "loadingInstance==");
  },
  // 关闭遮罩层
  closeLoading: () => {
    console.log(loadingInstance, "loadingInstance");
    loadingInstance.close();
  },

  // 美化打印实现方法
  info: (textOrTitle, content = "") => {
    const title = isEmpty(content) ? "Info" : textOrTitle;
    const text = isEmpty(content) ? textOrTitle : content;
    prettyPrint(title, text, "#909399");
  },
  error: (textOrTitle, content = "") => {
    const title = isEmpty(content) ? "Error" : textOrTitle;
    const text = isEmpty(content) ? textOrTitle : content;
    prettyPrint(title, text, "#F56C6C");
  },
  warning: (textOrTitle, content = "") => {
    const title = isEmpty(content) ? "Warning" : textOrTitle;
    const text = isEmpty(content) ? textOrTitle : content;
    prettyPrint(title, text, "#E6A23C");
  },
  success: (textOrTitle, content = "") => {
    const title = isEmpty(content) ? "Success " : textOrTitle;
    const text = isEmpty(content) ? textOrTitle : content;
    prettyPrint(title, text, "#67C23A");
  },
};

export default xh;
